
package com.wa.nashorn;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TimeUnit;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractRawTransferNashornScript<O> implements NashornScript<O> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRawTransferNashornScript.class);
    private LinkedTransferQueue<Object> queue;

    public AbstractRawTransferNashornScript(LinkedTransferQueue<Object> queue) {
        this.queue = queue;
    }

    public O process(NashornEngineService input) {
        ScriptObjectMirror objectMirror = input.getScriptObjectMirror(this.getScriptName(), this);
        return this.processInternal(objectMirror);
    }

    abstract String getScriptName();

    abstract O processInternal(ScriptObjectMirror objectMirror);

    public void onComplete(O out) {
        try {
            if (this.queue != null) {
                this.queue.tryTransfer(out, 10L, TimeUnit.SECONDS);
            }
        } catch (Exception var3) {
            LOGGER.error("transfer queue error", var3);
        }

    }

    public void onError(Exception ex) {
        try {
            if (this.queue != null) {
                this.queue.tryTransfer(ex, 10L, TimeUnit.SECONDS);
            }
        } catch (Exception var3) {
            LOGGER.error("transfer queue error", var3);
        }

    }
}
